using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;

namespace Lincolnwood.Foundation.Scheduling
{
    public sealed class WeeklyRecurrencePattern : RecurrencePattern
    {

        private WeeklyRecurrencePattern(): base()
        { }

        public WeeklyRecurrencePattern( DateTime endDate, int weekInterval)
            : base(endDate, weekInterval)
        {
        }

        public int WeekInterval
        {
            get { return this.Interval; }
        }

        public override DateTime FindClosestRecurrenceDate(RecurrableEventDefinition eventContext, DateTime expandBegin)
        {
            // Get the difference in weeks between the start of the expansion period and the start of the recurring event period.
            Double dateDifferenceWeeks = expandBegin.Subtract(eventContext.StartTime).TotalDays / 7; // DateAndTime.DateDiff(DateInterval.WeekOfYear, this.StartDate, expandBegin, FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1);
            
            // Compute how many recurrence intervals fall in the difference.
            // and round the result up to the closest whole number.
            Double exactIntervals = dateDifferenceWeeks / (Double)this.Interval;
            int closestWholeIntervals = (int)Math.Ceiling(exactIntervals);

            DateTime closestRecurrence = eventContext.StartTime.AddDays(7 * this.Interval * closestWholeIntervals);

            if (closestRecurrence < eventContext.StartTime)
                closestRecurrence = eventContext.StartTime;

            return closestRecurrence;
        }

        public override DateTime GetNextRecurrenceDate(DateTime baseDate)
        {
            return baseDate.AddDays(this.Interval * 7);
        }

        //public override IList<CalendarEvent> Expand(DateTime expandBegin, DateTime expandEnd)
        //{
        //    CalendarEvent evt = this.OwnerEvent;

        //    List<CalendarEvent> evtList = new List<CalendarEvent>();

        //    // create new CalendarEvents: 1 event per weekInterval on the weekDay of the week

        //    DateTime closestRecurrenceDate = this.FindClosestRecurrenceDate(expandBegin);

        //    if (this.StartDate <= expandEnd && expandBegin <= this.EndDate)
        //    {
        //        for (DateTime currentDate = closestRecurrenceDate;
        //            currentDate <= this.EndDate && currentDate <= expandEnd;
        //            currentDate = currentDate.AddDays(this.Interval * 7))
        //        {
        //            bool foundException = false;
        //            TimeSpan eventLength = evt.EndTime.Subtract(evt.StartTime);
        //            DateTime eventStartTime = currentDate;
        //            DateTime eventEndTime = currentDate.Add(eventLength);

        //            // check to see if this expanded event instance has an 
        //            // exception associated with it whose OriginalDate matches
        //            // the expanded event instance start date.
        //            for (int idx = 0; idx < this.Exceptions.Count; idx++)
        //            {
        //                // we can safely assume that there are no exceptions
        //                // if we reach the end of the list of sorted
        //                // exceptions without finding one or if we hit an event
        //                // with a start time greater than the expanded event instance's
        //                if (this.Exceptions[idx].OriginalDate > eventStartTime)
        //                    break;
        //                else
        //                {
        //                    // otherwise, if we find a match between the original date of the 
        //                    // exception and the currently expanded event instance
        //                    // and it is not deleted then add the exception
        //                    if (this.Exceptions[idx].OriginalDate.Date == eventStartTime.Date)
        //                    {
        //                        foundException = true;

        //                        if (!Exceptions[idx].Deleted &&
        //                                Exceptions[idx].StartTime <= expandEnd &&
        //                                Exceptions[idx].StartTime >= expandBegin)
        //                            evtList.Add(this.Exceptions[idx]);
        //                    }

        //                }
        //            }
        //            if (!foundException)
        //            {
        //                // if no exception is associated with this event then use the generic recurring event
        //                CalendarEvent recurringEvt = new CalendarEvent(evt.Id, evt.Name, evt.Description, eventStartTime, eventEndTime);
        //                recurringEvt.Recurrence = this;
                        
        //                evtList.Add(recurringEvt);
        //            }
        //        }
        //    }
        //    return evtList;
        //}
    }
}